#pragma once
#include <iostream>
#include <vector>
using namespace std;

namespace test
{
	template<class T>
	class myless
	{
	public:
		bool operator()(const T& a, const T& b)
		{
			return a < b;
		}
	};
	template<class T>
	class mygreater
	{
	public:
		bool operator()(const T& a, const T& b)
		{
			return a > b;
		}
	};
	template<class T,class Container = vector<T>,class func = myless<T>>
	class Priority_queue
	{
	public:
		Priority_queue() = default;
		template<class Inputinitial>
		Priority_queue(Inputinitial first, Inputinitial last)
		{
			while (first != last)
			{
				_container.push_back(*first);
				first++;
			}
			for (int i = (_container.size() - 1 - 1) / 2; i >= 0; i--)
			{
				adjustDown(i);
			}
		}
		void push(const T& x)
		{
			_container.push_back(x);
			adjustUP(_container.size() - 1);
		}
		void pop()
		{
			swap(_container[0], _container[_container.size() - 1]);
			_container.pop_back();
			adjustDown(0);
		}
		const T& top()
		{
			return _container[0];
		}
		int size()
		{
			return _container.size();
		}
		bool empty()
		{
			return _container.empty();
		}
	private:
		void adjustUP(int child)
		{
			func  comp;
			int parent = (child - 1) / 2;
			while (child > 0)
			{
				if (comp(_container[parent], _container[child]))
				{
					swap(_container[parent], _container[child]);
					child = parent;
					parent = (child - 1) / 2;
				}
				else
				{
					break;
				}
			}
		}
		void adjustDown(int parent)
		{
			func  comp;

			int child = parent * 2 + 1;
			while (child < _container.size())
			{
				if (child + 1 < _container.size() && func(_container[child], _container[child+1]))
				{
					child++;
				}
				if (comp(_container[parent], _container[child]))
				{
					swap(_container[parent], _container[child]);
				    parent = child;
					child = parent * 2 + 1;
				}
				else
				{
					break;
				}
			}
		}
	private:
		Container _container;
	};
}